//***********************************************************
//*
//* File:           PlayerWrapper.java
//* Author:         Abhinav Kamra
//* Contact:        kamra@cs.columbia.edu
//* Update:         10.16.2003
//*
//* Description:    Compositional wrapper for IFCPlayer
//*                 objects.
//*
//***********************************************************

package paths;

import java.awt.Color;

public final class PlayerWrapper implements IFCPlayer {

	double        _score;
	IFCPlayer     _player;
	Class         _class;
	Maps    _maps;


	PlayerWrapper(Class __class) throws Exception {
        _class = __class;
	}
    
    private void register_priv(Maps __maps) throws Exception {
        try {
            _maps = __maps;
            _player.register(_maps);
        } catch (Exception EXC) {
            System.out.println(EXC.getMessage());
            //EXC.printStackTrace();
	    System.out.println("Player " + _class + " threw an Exception in register()");
        }
    }


	public void register(Maps __maps) throws Exception {
        _player = (IFCPlayer) _class.newInstance();
        register_priv(__maps);

    }

    public void register(Maps __maps, IFCPlayer __player) throws Exception {
        _player = __player;
        _class = _player.getClass();
        register_priv(__maps);
    }

	public String name() throws Exception {
        try {
	        return _player.name();
        } catch (Exception EXC) {
            System.out.println(EXC.getMessage());
            EXC.printStackTrace();
	    System.out.println("Player " + _class + " threw an Exception in name()");
	    return "Anonymous";
        }
    }
    
    public Color color() throws Exception {
        try {
            return _player.color();
        } catch (Exception EXC) {
            System.out.println(EXC.getMessage());
            EXC.printStackTrace();
	    System.out.println("Player " + _class + " threw an Exception in color()");
	    return new Color(1.0f, 1.0f, 0.9f);
        }
    }

    public Class playerClass() throws Exception {
        return _class;
    }

    public void gameOver() throws Exception {
        try {
            if (_player instanceof IFCPersistentPlayer) {
                ((IFCPersistentPlayer) _player).gameOver();
            }
        } catch (Exception EXC) {
            System.out.println(EXC.getMessage());
            EXC.printStackTrace();
	    System.out.println("Player " + _class + " threw an Exception in gameOver()");
        }
    }

    public Move move() throws Exception {
        try {
            Move RET = _player.move();
            return RET;

        } catch (Exception EXC) {
            System.out.println(EXC.getMessage());
            //EXC.printStackTrace();
	    System.out.println("Player " + _class + " threw an Exception in move()");
            return null;
        }
    }

    public boolean interactive() throws Exception {
        try {
            return _player.interactive();
        } catch (Exception EXC) {
            System.out.println(EXC.getMessage());
            EXC.printStackTrace();
	    System.out.println("Player " + _class + " threw an Exception in interactive()");
	    return false;
        }
    }

	public double score() {
	    return _score;
    }

    public void setScore(double __score) {
        _score = __score;
    }

    public IFCPlayer player() {
        return _player;
    }

    public String toString() {
	    try {
    		StringBuffer SB = new StringBuffer("[");

            SB.append(_player.name());
		    SB.append("]");
    		return new String(SB);
        } catch (Exception EXC) {
            return EXC.getMessage();
        }
	}
}
